﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace BDPAutodetect.Client
{
    /// <summary>
    /// InputNameDialog.xaml 的交互逻辑
    /// </summary>
    [ObservableObject]
    public partial class InputNameDialog : Window, IDialog<string>
    {
        /// <summary>
        /// 当前编辑参数界限数据
        /// </summary>
        [ObservableProperty]
        protected string inputName = string.Empty;
        /// <summary>
        /// 显示状态信息
        /// </summary>
        [ObservableProperty]
        protected string statusMsg = string.Empty;
        /// <summary>
        /// 
        /// </summary>
        protected List<string>? InputNameChecks { get; set; }
        /// <summary>
        /// 返回结果信息
        /// </summary>
        public string? DialogResultData { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="argName">名称</param>
        /// <param name="argNameChecks">名称检查集合</param>
        public InputNameDialog(string argName, List<string>? argNameChecks)
        {
            InitializeComponent();
            this.InputName = argName;
            this.InputNameChecks = argNameChecks;
            this.DataContext = this;
        }

        /// <summary>
        /// 确认选择事件
        /// </summary>
        [RelayCommand]
        protected void ExecuteConfirmOk()
        {
            if (!this.Check()) return;
            this.DialogResult = true;
            this.DialogResultData = this.InputName.Trim();
            this.Close();
        }

        /// <summary>
        /// 取消事件
        /// </summary>
        [RelayCommand]
        protected void ExecuteConfirmCancel()
        {
            this.DialogResult = false;
            this.Close();
        }

        /// <summary>
        /// 检查
        /// </summary>
        /// <returns></returns>
        protected bool Check()
        {
            if (string.IsNullOrWhiteSpace(this.InputName))
            {
                this.StatusMsg = "名称不能为空!";
                return false;
            }
            if (this.InputNameChecks != null && this.InputNameChecks.Count > 0
                && this.InputNameChecks.Exists(v => v.Equals(this.InputName.Trim(), StringComparison.OrdinalIgnoreCase)))
            {
                this.StatusMsg = "名称已存在!";
                return false;
            }
            this.StatusMsg = string.Empty;
            return true;
        }
    }
}
